﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommandExecuter;
using PHPGenerator;

namespace PHPGenerator
{
    class PHPGenerator
    {
        private bool activeif = false;
        public void GenerateCode(string source, string exportpath)
        {
            StreamReader sr = new StreamReader(source);
            string fullcode = sr.ReadToEnd();
            sr.Close();
            string[] lines = fullcode.Split(new string[] {"\r\n"}, StringSplitOptions.None);
            using (StreamWriter sw = File.AppendText(exportpath))
            {
                foreach (string line in lines)
                {
                    sw.WriteLine(this.ExecuteLine(line));
                }
                sw.Close();
            }
        }
        private string ExecuteLine(string line)
        {
            string retstr = "";
            string[] spl = line.Split(' ');
            switch (spl[0])
            { 
                case "import":
                    retstr = this.Import(spl);
                    break;
                case "display":
                    retstr = this.Display(spl);
                    break;
                case "post":
                    retstr = this.POST(spl);
                    break;
                case "get":
                    retstr = this.GET(spl);
                    break;
                case "IF":
                case "if":
                case "If":
                    retstr += this.IF(spl);
                    break;
                case "ELSE":
                case "Else":
                case "else":
                    retstr += this.ELSE(spl);
                    break;
            }
            if (spl[0].StartsWith("#") || spl[0].StartsWith("A#"))
            {
                retstr = this.Var(spl);
            }
            else if (this.IsArrayIndex(spl[0]))
            { 
                
            }
            return retstr;

        }
        private bool IsAlphabetical(string stri)
        {
            bool done = false;
            string str = stri.ToUpper();
            if (str == "TRUE" || str == "FALSE")
            {
                done = false;
            }
            else
            {
                char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
                foreach (char a in alpha)
                {
                    if (str.Contains(System.Convert.ToString(a)))
                    {
                        done = true;
                        break;
                    }
                    else
                        continue;
                }
            }
            return done;
        }
        private bool IsArrayIndex(string stri)
        {
            bool done = false;
            string str = stri.ToUpper();
            char[] nums = "123456789".ToCharArray();
            foreach (char n in nums)
            {
                if (str.StartsWith(System.Convert.ToString(n)))
                {
                    done = true;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return done;
        }
        private string Import(string[] spl)
        {
            string retstr = "include \'";
            string fullarg = "";
            for (int i = 1; i <= spl.Length - 1; i++)
            {
                if (fullarg == "")
                    fullarg += spl[i];
                else
                    fullarg = fullarg + " " + spl[i];
            }
            retstr = retstr + fullarg + "\';";
            return retstr;
        }
        private string Display(string[] spl)
        {
            string retstr = "echo ";
            string fullarg = "";
            for (int i = 1; i <= spl.Length - 1; i++)
            {
                if (fullarg == "")
                    fullarg += spl[i];
                else
                    fullarg = fullarg + " " + spl[i];
            }
            retstr += this.VarSecArg(fullarg);
            return retstr;
        }
        private string Var(string[] spl)
        {
            string retstr = "";
            string fullarg = "";
            string ret = "";
            bool asd = false;
            if (spl[0].StartsWith("A"))
            {
                ret = spl[0].Remove(0, 1);
                ret = ret.Replace('#', '$');
                asd = true;
            }
            else
            {
                ret = spl[0].Replace('#', '$');
            }
            retstr = retstr + ret + " = ";
            string sffa = "";
            for(int i = 2;i<=spl.Length-1;i++)
            {
                if(sffa=="")
                    sffa += spl[i];
                else
                    sffa += " " + spl[i];
            }
            string sff = sffa.Replace(',', '☺');
            if (spl[2].StartsWith("#") && !spl[2].Contains(","))
            {
                ret = spl[2].Replace('#', '$');
                retstr = retstr + ret + ";";
            }
            else if(asd)
            {
                retstr = retstr + "arraay(";
                if(sff.Contains("|"))
                {
                
                    string[] arritems = sff.Split('|');
                    int a = 0;
                    foreach(string arritem in arritems)
                    {
                        if (this.IsAlphabetical(arritem))
                        {
                            if (a == 0)
                                retstr = retstr + System.Convert.ToString(a) + " => \"" + arritem + "\"";
                            else
                                retstr = retstr + ", " + System.Convert.ToString(a) + " => \"" + arritem + "\"";
                        }
                        else
                        {
                            if (a == 0)
                                retstr = retstr + System.Convert.ToString(a) + " => " + arritem;
                            else
                                retstr = retstr + ", " + System.Convert.ToString(a) + " => " + arritem;
                        }
                        a++;
                    }
                }
                retstr += ");";
            }
            else
            {
                for (int i = 2; i <= spl.Length - 1; i++)
                {
                    if (fullarg == "")
                        fullarg += spl[i];
                    else
                        fullarg = fullarg + " " + spl[i];
                }
                if (this.IsAlphabetical(fullarg))
                    retstr = retstr + "\"" + fullarg + "\";";
                else
                    retstr += fullarg;
            }

            return retstr;
        }
        private string VarSecArg(string nextarg)
        {
            string retstr = "";
            string ret = "";
            if (nextarg.StartsWith("#"))
            {
                ret = nextarg.Replace('#', '$');
                retstr = retstr + ret + ";";
            }
            else
            {
                if (this.IsAlphabetical(nextarg))
                    retstr = retstr + "\"" + nextarg + "\";";
                else
                    retstr += nextarg;
            }
            return retstr;
        }
        private string POST(string[] spl)
        {
            string retstr = "$_POST['";
            string fullarg = "";
            for (int i = 1; i <= spl.Length - 1; i++)
            {
                if (fullarg == "")
                    fullarg += spl[i];
                else
                    fullarg = fullarg + " " + spl[i];
            }
            retstr += fullarg;
            return retstr;
        }
            private string GET(string[] spl)
        { 
            string retstr = "$_GET['";
            string fullarg = "";
            for (int i = 1; i <= spl.Length - 1; i++)

            {
                if (fullarg == "")
                    fullarg += spl[i];
                else
                    fullarg = fullarg + " " + spl[i];
            }
            retstr += fullarg;
            return retstr;
        }
            private string IF(string[] spl)
            {
                string btr = "";
                bool d = false;
                string retstr = "";
                retstr += "if(";
                string exs = "";
                string bret = "";
                for (int a = 1; a <= spl.Length - 1; a++)
                {
                    if (spl[a] == "THEN" || spl[a] == "Then" || spl[a] == "then")
                    {
                        break;
                    }
                    else
                    {
                        if (exs == "")
                            exs += spl[a];
                        else
                            exs += " " + spl[a];
                    }
                }
                if (!exs.Contains(" "))
                    retstr += exs;
                else
                {
                    string bretr = exs.Replace('#', '$');
                    retstr = retstr + bretr;
                }
                for (int a = 1; a <= spl.Length - 1; a++)
                {
                    if (d)
                    {
                        if (btr == "")
                            btr += spl[a];
                        else
                            btr += " " + spl[a];
                    }
                    else
                    {
                        if (spl[a] == "THEN" || spl[a] == "Then" || spl[a] == "then")
                        {
                            d = true;
                        }
                        else
                            continue;
                    }
                }
                string ubtr = btr.Replace('#','$');

                retstr += ")\r\n{\r\n" + ubtr + ";\r\n}";
                this.activeif = true;
                return retstr;
            }
            private string ELSE(string[] spl)
            {
                string retstr = "";
                if (this.activeif)
                {
                    retstr += "else\r\n{\r\n";
                    string fa = "";
                    for (int i = 1; i <= spl.Length - 1; i++)
                    { 
                        if(fa=="")
                            fa += spl[i];
                        else
                            fa += " " + spl[i];
                    }
                    PHPGenerator p = new PHPGenerator();
                    retstr += p.ExecuteLine(fa);
                    this.activeif = false;
                    retstr += "\r\n}\r\n";
                }

                return retstr;
            }
    }
}